#include "from.h"

#include "fromcomponent.h"

From::~From()
{
    if(components != NULL){
        for(unsigned int i=0; i<components->size(); i++){
            delete components->at(i);
        }//for
        delete components; components = NULL;
    }// if
}

From::From(const From& other)
{
    if(other.components == NULL) components = NULL;
    else{
        components = new vector<FromComponent *>();
        for(unsigned int i=0; i<other.components->size(); i++){
            components->push_back(other.components->at(i)->clone());
        }//for
    }// if-else
}

From::From(vector<FromComponent *> * components)
{
    if(components == NULL) this->components = NULL;
    else{
        this->components = new vector<FromComponent *>();
        for(unsigned int i=0; i<components->size(); i++){
            this->components->push_back(components->at(i)->clone());
        }//for
    }// if-else
}

vector<FromComponent *> * From::getComponents() const
{
    return components;
}

void From::setComponents(vector<FromComponent *> * components)
{
    for(unsigned int i=0; i<this->components->size(); i++){
        delete this->components->at(i);
    }//for
    delete this->components; this->components = NULL; // NULL

    if(components != NULL){
        this->components = new vector<FromComponent *>();
        for(unsigned int i=0; i<components->size(); i++){
            this->components->push_back(components->at(i)->clone());
        }//for
    }//if
}

void From::addFromComponent(FromComponent * component)
{
    if(components == NULL) components = new vector<FromComponent *>();
    if(component != NULL) components->push_back(component->clone());
}

string From::toString()
{
    string str = "[";
    if(components != NULL){
        for(unsigned int i=0; i<components->size(); i++){
            str = str + components->at(i)->toString() + ",";
        }// for
        str.erase(str.length()-1);
    }// if
    str = str + "]";
    return str;
}

From * From::clone()
{
    return new From(*this);
}

